package com.example.controller;

import com.example.entity.Post;
import com.example.service.PostService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 文章控制器
 * 提供文章相关的 REST API
 * 
 * @author example
 * @since 2024-01-01
 */
@Slf4j
@RestController
@RequestMapping("/posts")
@RequiredArgsConstructor
public class PostController {

    private final PostService postService;

    /**
     * 获取所有文章
     */
    @GetMapping
    public ResponseEntity<List<Post>> getAllPosts() {
        log.info("请求获取所有文章");
        List<Post> posts = postService.getAllPosts();
        return ResponseEntity.ok(posts);
    }

    /**
     * 根据 ID 获取文章
     */
    @GetMapping("/{id}")
    public ResponseEntity<Post> getPostById(@PathVariable Long id) {
        log.info("请求获取文章，ID: {}", id);
        Post post = postService.getPostById(id);
        if (post != null) {
            return ResponseEntity.ok(post);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 根据用户 ID 获取文章列表
     */
    @GetMapping("/user/{userId}")
    public ResponseEntity<List<Post>> getPostsByUser(@PathVariable Long userId) {
        log.info("请求获取用户文章，用户ID: {}", userId);
        List<Post> posts = postService.getPostsByUser(userId);
        return ResponseEntity.ok(posts);
    }

    /**
     * 创建文章
     */
    @PostMapping
    public ResponseEntity<Post> createPost(@RequestBody Post post) {
        log.info("请求创建文章: {}", post.getTitle());
        Post createdPost = postService.createPost(post);
        if (createdPost != null) {
            return ResponseEntity.ok(createdPost);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    /**
     * 使用表单方式创建文章
     */
    @PostMapping("/form")
    public ResponseEntity<Post> createPostWithForm(
            @RequestParam Long userId,
            @RequestParam String title,
            @RequestParam String body) {
        log.info("使用表单请求创建文章: {}", title);
        Post createdPost = postService.createPostWithForm(userId, title, body);
        if (createdPost != null) {
            return ResponseEntity.ok(createdPost);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    /**
     * 更新文章
     */
    @PutMapping("/{id}")
    public ResponseEntity<Post> updatePost(@PathVariable Long id, @RequestBody Post post) {
        log.info("请求更新文章，ID: {}", id);
        Post updatedPost = postService.updatePost(id, post);
        if (updatedPost != null) {
            return ResponseEntity.ok(updatedPost);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 删除文章
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deletePost(@PathVariable Long id) {
        log.info("请求删除文章，ID: {}", id);
        boolean deleted = postService.deletePost(id);
        if (deleted) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 文件上传示例
     */
    @PostMapping("/upload")
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
        log.info("请求上传文件: {}", file.getOriginalFilename());
        String result = postService.uploadFile(file);
        if (result != null) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.badRequest().body("文件上传失败");
        }
    }

    /**
     * 多文件上传示例
     */
    @PostMapping("/upload/multiple")
    public ResponseEntity<String> uploadMultipleFiles(
            @RequestParam("description") String description,
            @RequestParam("files") List<MultipartFile> files) {
        log.info("请求上传多个文件，数量: {}", files.size());
        String result = postService.uploadMultipleFiles(description, files);
        if (result != null) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.badRequest().body("多文件上传失败");
        }
    }

    /**
     * 演示文章功能
     */
    @GetMapping("/demo")
    public ResponseEntity<String> demonstratePostFeatures() {
        log.info("演示文章功能");
        postService.demonstratePostFeatures();
        return ResponseEntity.ok("文章功能演示完成");
    }
} 